

/*********************************************************************************/
/*                                                                               */ 
/* Create sample with individuals who received debt relief and those who did not */
/*                                                                               */ 
/*********************************************************************************/


libname raw 'G:\Data\Rawdata\707677';

libname stats 'G:\Data\Rawdata\707677\Eksterne data'; 

libname derived 'G:\Data\Workdata\707677\qod7677\Gustaf\Data\Derived';

libname common 'G:\Data\Workdata\707677\Common\01Data\01SAS';






/***************************************************/
/* Create data set with messages from Statstidende */
/***************************************************/



data stats;

set stats.dst;

run;



data population;

set stats.population;

keep person_id pnr;

run;



proc sql;

create table stats as select *

from stats as a left join

population as b on

a.person_id=b.person_id;

quit;



data stats;

retain pnr year month court decision judgeid lawyerid person_id;

set stats;

run;



proc sort data=stats;

by pnr year month person_id;

run;



data common.stats;

set stats;

run;



proc datasets library=work nolist;

delete population;

quit;







/*****************************/
/* Create data on applicants */
/*****************************/


data list;

set stats;

keep pnr decision year month person_id;

run;



data list;

set list;

rename decision=decision_long;

run;



data list;

set list;

length decision $2.;

if decision_long=:'P' then decision='P';

if decision_long=:'I' then decision='I';

if decision_long=:'K' then decision='K';

if decision_long=:'T' then decision='T';

run;



data list;

set list;

drop decision_long;

run;



data list;

retain pnr year month decision;

set list;

run;



proc sort data=list;

by pnr year month person_id;

run;





/**************************************/
/* Count number of entries per person */
/**************************************/



data list;

set list;

by pnr;

count+1;

if first.pnr then count=1;

run;



data last;

set list;

by pnr;

if last.pnr then output;

run;



data last;

set last;

keep pnr count;

rename count=entries;

run;



proc sql;

create table list as select *

from list (drop=count) as a left join

last as b on

a.pnr=b.pnr;

quit;




proc datasets library=work nolist;

delete last;

quit;





/********************************/
/* Compute time between entries */
/********************************/


data list;

set list;

date=mdy(month,15,year);

run;



data list;

set list;

format date YYMMDD10.;

run;



proc summary data=list;

class pnr;

output out = stats

max(date)=lastdate

min(date)=firstdate;

run;


data stats;

set stats;

drop _type_ _freq_;

if pnr~='';

tottime=(lastdate-firstdate)/365.25;

run;



data stats;

set stats;

tottime=ROUND(tottime,0.01);

run;



proc sql;

create table list as select *

from list as a left join

stats as b on

a.pnr=b.pnr;

quit;



proc sort data=list;

by pnr year month person_id;

run;



/***********************************/
/* Identify sequences of decisions */
/***********************************/


data list;

set list;

count+1;

by pnr;

if first.pnr then count=1;

run;



proc summary data=list;

output out= stats

max(count)=maxnum;

run;



data stats;

set stats;

drop _type_ _freq_;

run;



data stats;

set stats;

CALL SYMPUT('maxcount',maxnum);

run;



data list;

set list;

length sequence bsequence $50.;

run;



%macro decision(num);


data d&num.;

set list;

if count=&num.;

keep pnr decision date;

rename decision=d&num.;

rename date=t&num.;

run;



proc sql;

create table list as select *

from list as a left join

d&num. as b on

a.pnr=b.pnr;

quit;



data list;

set list;

sequence=CATT(OF sequence d&num.);

bsequence=CATT(OF d&num. bsequence); 

run;



data list;

set list;

drop d&num.;

run;



proc datasets library=work nolist;

delete d&num.;

quit;


%mend;




%macro repeat;

%do i=1 %to &maxcount.;

%decision(&i);

%end;

%mend;



%repeat;




/*********************************************/
/* Clean variables for sequences of decision */
/*********************************************/


data list;

set list;

sequence=COMPRESS(sequence);

bsequence=COMPRESS(bsequence);

run;



data list;

set list;

length seq bseq $&maxcount.;

seq=sequence;

bseq=bsequence;

run;



data list;

set list;

drop sequence bsequence;

run;



/**********************************/
/* Only keep one entry per person */
/**********************************/


proc sort data=list;

by pnr year month person_id;

run;



proc sort data=list nodupkey;

by pnr;

run;





/*******************************************************************************/
/*                                                                             */
/* Classify individuals into those who receive debt relief and those who don't */
/*                                                                             */
/*******************************************************************************/



/* Identify sequences with a Kendelse and with a Tilbagekaldelse after a Kendelse */


data list;

set list;

kb=find(bseq,"K");

tb=find(bseq,"T");

kb_first=find(seq,"K");

run;



data list;

set list;

kendelse=0;

tilbage_post_kendelse=0;

if kb>0 then kendelse=1;

if kb>0 and tb>0 and tb>kb then tilbage_post_kendelse=1;

run;



/* Define those granted debt relief as those who have a Kendelse */


data list;

set list;

granted=kendelse;

run;



/* Mark those granted debt relief where there is no subsequent Tilbagekaldelse */


data list;

set list;

granted_not_revoked=0;

if granted=1 and tilbage_post_kendelse=0 then granted_not_revoked=1;

run;





/* Tabulate fraction granted debt relief (main definition) and fraction granted debt relief without being revoked (alternative definition) */


proc freq data=list;

tables granted granted_not_revoked;

run;



data list;

retain pnr year month decision person_id entries date firstdate lastdate tottime count seq bseq kb tb granted granted_not_revoked;

set list;

run;




/* Identify time for first Kendelse */


data list;

set list;

ls=length(seq);

run;



data list;

set list;

kpos_first=kb_first;

run;



data list;

set list;

format kdate_first YYMMDD10.;

run;



%macro position(num);


data list;

set list;

if &num.=kpos_first then kdate_first=t&num.;

run;


%mend;



%macro repeat_position;

%do i=1 %to &maxcount.;

%position(&i.);

%end;

%mend;



%repeat_position;




data list;

retain pnr year month decision person_id entries date firstdate lastdate tottime count seq bseq kb tb granted kdate_first granted_not_revoked;

set list;

run;



data list;

set list;

drop year month decision date count;

run; 






/* Describe time interval from proklama to kendlse in months */


data list;

set list;

time_gap=(kdate_first-firstdate)/(365.25/12);

run;



proc summary data=list;

output out = medstats

median(time_gap)=medtime

p90(time_gap)=p90time;

run;




data medstats;

set medstats;

drop _type_ _freq_;

run;






/* Store year when investigation is opened (when this information is available and does not have to be imputed) */


data list;

set list;

if substr(seq,1,1)='P' then year_applied=year(firstdate);

run;





/**************************************************************************************/
/* Compute fraction of individuals who were granted debt relief as a function of time */
/**************************************************************************************/



proc sort data=list;

by year_applied;

run;



proc summary data=list;

by year_applied;

output out=meanstats

mean(granted)=mean_granted;

run;



data meanstats;

set meanstats;

drop _type_ _freq_;

run;




/* Export data */


proc export data=meanstats outfile='G:\Data\Workdata\707677\Common\01Data\02STATA\meangrant.dta' DBMS=STATA replace;

run;





/* Save data on who was granted debt relief */



proc sort data=list nodupkey;

by pnr;

run;




data list;

set list;

pyear=year(firstdate);

pmonth=month(firstdate);

run;





data list;

retain pnr granted granted_not_revoked seq pyear pmonth firstdate lastdate kdate_first tottime; 

set list;

keep pnr granted granted_not_revoked seq pyear pmonth firstdate lastdate kdate_first tottime; 

run;





/* Add data on death dates */



data death;

set raw.doede;

keep pnr doddato;

run;



proc datasets library=work nolist;

modify death;

attrib _all_ label='';

quit;



data death;

set death;

FORMAT deathdate YYMMDD10.;

deathdate=doddato;

run;



proc sql;

create table list as select *

from list as a left join

death as b on

a.pnr=b.pnr;

quit;



data list;

set list;

drop doddato;

run;




data list;

set list;

deathtime=ROUND((deathdate-firstdate)/365.25,0.01);

run;



data list;

set list;

death_before=0;

if deathtime~=. and deathtime<0 then death_before=1;

run;



proc freq data=list;

tables death_before;

run;






/* Save data */


data common.applicants;

set list;

run;




proc datasets library=work nolist kill;

quit;























































